package schema

import (
	"time"

	"entgo.io/ent"
	"entgo.io/ent/dialect/entsql"
	"entgo.io/ent/schema"
	"entgo.io/ent/schema/edge"
	"entgo.io/ent/schema/field"
	"entgo.io/ent/schema/index"
)

// ActivityAttachment 活动附件
type ActivityAttachment struct {
	ent.Schema
}

// Fields 活动附件字段
func (ActivityAttachment) Fields() []ent.Field {
	return []ent.Field{
		field.Int64("id").
			Positive().
			Unique().
			Immutable(),
		// 显式定义外键字段
		field.Int64("activity_id").
			Comment("活动ID"),
		field.String("file_name").
			NotEmpty().
			MaxLen(255).
			Comment("文件名"),
		field.String("file_path").
			NotEmpty().
			MaxLen(255).
			Comment("文件路径"),
		field.Int64("file_size").
			Comment("文件大小(字节)"),
		field.String("file_type").
			NotEmpty().
			MaxLen(100).
			Comment("文件类型"),
		field.Time("uploaded_at").
			Default(time.Now).
			Comment("上传时间"),
		// 显式定义外键字段
		field.Int64("uploaded_by").
			Comment("上传者ID"),
	}
}

// Edges 活动附件关联
func (ActivityAttachment) Edges() []ent.Edge {
	return []ent.Edge{
		// activity_id 字段不是 Optional().Nillable()，所以边需要 Required()
		edge.From("activity", Activity.Type).
			Ref("attachments").
			Field("activity_id").
			Unique().
			Required(). // 添加 Required()
			Comment("所属活动"),
		// uploaded_by 字段不是 Optional().Nillable()，所以边需要 Required()
		edge.From("uploader", User.Type).
			Ref("uploaded_attachments").
			Field("uploaded_by").
			Unique().
			Required(). // 添加 Required()
			Comment("上传者"),
	}
}

// Indexes 索引
func (ActivityAttachment) Indexes() []ent.Index {
	return []ent.Index{
		index.Fields("activity_id"),
		index.Fields("uploaded_by"),
	}
}

// Annotations of the ConversationMember.
func (ActivityAttachment) Annotations() []schema.Annotation {
	return []schema.Annotation{
		entsql.Annotation{
			Table: "act_attachments",
		},
	}
}
